package com.java.tutorials.spring.ProSpring4.ch8.hibernateEnvers;

import org.springframework.context.support.GenericXmlApplicationContext;

import java.util.Date;
import java.util.List;

public class SpringJPASample {
    public static void main(String[] args) {
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
        ctx.load("classpath:ProSpring4/ch8/hibernateEnvers/app-context-annotation.xml");
        ctx.refresh();

        ContactAuditService contactService = ctx.getBean(
            "contactAuditService", ContactAuditService.class);

        ContactAudit contactAudit1 = contactService.findById(2L);
        contactAudit1.setFirstName("xu");
        contactAudit1.setLastName("zhike");
        contactAudit1.setBirthDate(new Date());
        contactService.save(contactAudit1);

//        System.out.println("Add new contact");
//        ContactAudit contact = new ContactAudit();
//        contact.setFirstName("Michael");
//        contact.setLastName("Jackson");
//        contact.setBirthDate(new Date());
//        contactService.save(contact);
//        listContacts(contactService.findAll());
//
//        System.out.println("Update contact");
//        contact.setFirstName("Tom");
//        contactService.save(contact);
//        listContacts(contactService.findAll());
//
        ContactAudit oldContact = contactService.findAuditByRevision(1l, 1);
//        System.out.println("");
//        System.out.println("Old Contact with id 1 and rev 1:" + oldContact);
//        System.out.println("");
//        oldContact = contactService.findAuditByRevision(1l, 2);
//        System.out.println("");
//        System.out.println("Old Contact with id 1 and rev 2:" + oldContact);
//        System.out.println("");
    }

    private static void listContacts(List<ContactAudit> contacts) {
        System.out.println("");
        System.out.println("Listing contacts without details:");
        for (ContactAudit contact: contacts) {
            System.out.println(contact);
            System.out.println();
        }
    }
}
